package cn.t.repository.impl.device;

import cn.t.converter.device.ApkConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.device.ApkDO;
import cn.t.persistence.entity.device.ApkPO;
import cn.t.persistence.mapper.device.ApkMapper;
import cn.t.repository.device.ApkRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 安卓apk管理表(Apk)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:08
 */
@Slf4j
@Repository
@AllArgsConstructor
public class ApkRepositoryImpl extends RepositoryImpl<ApkMapper, ApkPO, ApkConverter, ApkDO> implements ApkRepository {


    @Override
    public List<ApkDO> findByModel(ApkDO apkDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize
                :
                " limit " + start + "," + pageSize;

        List<ApkPO> list = getWrapper(apkDO)
                .last(lastSql)
                .list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<ApkPO> getWrapper(ApkDO apkDO) {
        LambdaQueryChainWrapper<ApkPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(apkDO.getVersion())) {
            wrapper.eq(ApkPO::getVersion, apkDO.getVersion());
        }
        if (!ObjectUtils.isEmpty(apkDO.getAppId())) {
            wrapper.eq(ApkPO::getAppId, apkDO.getAppId());
        }
        if (!ObjectUtils.isEmpty(apkDO.getFileurl())) {
            wrapper.eq(ApkPO::getFileurl, apkDO.getFileurl());
        }
        if (!ObjectUtils.isEmpty(apkDO.getReleasenote())) {
            wrapper.eq(ApkPO::getReleasenote, apkDO.getReleasenote());
        }
        if (!ObjectUtils.isEmpty(apkDO.getGmtModified())) {
            wrapper.between(ApkPO::getGmtModified, apkDO.getGmtModifiedStart(), apkDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(apkDO.getGmtCreated())) {
            wrapper.between(ApkPO::getGmtCreated, apkDO.getGmtCreatedStart(), apkDO.getGmtCreatedEnd());
        }
        return wrapper;
    }

    @Override
    public long count(ApkDO apkDO) {
        return getWrapper(apkDO).count();
    }

    @Override
    public ApkDO findByVersion(Integer version) {
        LambdaQueryChainWrapper<ApkPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(version)) {
            wrapper.eq(ApkPO::getVersion, version);
        }
        List<ApkDO> collect = wrapper.list().stream().map(converter::po2Model).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return null;
        }
        return collect.get(0);
    }
}
